// Bachelor of Software Engineering 
// Media Design School 
// Auckland
// New Zealand
// 
// (c) 2005 - 2014 Media Design School 
// 
// File Name : CardPileGroup.cpp
// Description : CardPileGroup implementation
// Author : Serge Radinovich
// Mail : srad015@hotmail.com

#include "CardPileGroup.h"
#include "Game.h"

////////////////////////
//Default constructor
//All initialization is done in Tableau, Stock, Foundation
///////////////////////
CardPileGroup::CardPileGroup(void)
{
}

////////////////////////
//Destructor
//Clears all cards held in vector of lists
///////////////////////
CardPileGroup::~CardPileGroup(void)
{
	
	while(!m_Stacks.empty())
	{
		auto _stackIter = m_Stacks.begin();
		while(!(*_stackIter)->empty())
		{
			auto _cardIter = (*_stackIter)->begin();
			delete *_cardIter;
			*_cardIter = nullptr;
			_cardIter = (*_stackIter)->erase(_cardIter);
		}
		delete *_stackIter;
		_stackIter = m_Stacks.erase(_stackIter);
	}
}

////////////////////////
//Returns true if the vector stores no lists of cards
///////////////////////
bool CardPileGroup::IsEmpty() const
{
	for(UINT pile = 0; pile < m_Stacks.size(); pile++)
	{
		if(!m_Stacks[pile]->empty())
		{
			return false;
		}
	}
	return true;

}

////////////////////////
//Returns number of cards in all stacks combined
///////////////////////
int CardPileGroup::GetCardCount() const
{
	int _iCount = 0;
	for(UINT i = 0; i < m_Stacks.size(); i++)
	{
		_iCount += m_Stacks[i]->size();
	}

	return _iCount;
}

////////////////////////
//Returns true if bounding box collision detected with mouse click position
//If mouse click is not on front card, use an offset to prevent overlap problems
///////////////////////
bool CardPileGroup::CheckCollision(Vector2 _MouseClickPos, GameObject* _Card, bool _bFrontCard)
{
	int _iYOffSet = 0;
	if(!_bFrontCard)
	{
		_iYOffSet = Card::s_kiHeight - 20; //20 == Tableau::s_kiSpaceBetweenCards
	}
	
	
	if(_MouseClickPos.x < _Card->GetPos().x) return false;

	if(_MouseClickPos.x > _Card->GetPos().x + _Card->GetSize().width) return false;

	if(_MouseClickPos.y < _Card->GetPos().y) return false;

	if(_MouseClickPos.y > _Card->GetPos().y + _Card->GetSize().height - _iYOffSet) return false; //Account for offset on cards behind others

	return true;
}

////////////////////////
//Retruns true of bounding box collision detected between two gameobjects (cards typically)
///////////////////////
bool CardPileGroup::CheckCollision(GameObject* _pCard1, GameObject* _pCard2)
{
	
	if(_pCard1->GetPos().x + _pCard1->GetSize().width < _pCard2->GetPos().x) return false;

	if(_pCard1->GetPos().x > _pCard2->GetPos().x + _pCard2->GetSize().width) return false;
	
	if(_pCard1->GetPos().y + _pCard1->GetSize().height < _pCard2->GetPos().y) return false;

	if(_pCard1->GetPos().y > _pCard2->GetPos().y + _pCard2->GetSize().height) return false;

	return true;

}

////////////////////////
//Initializes the bitmap for nullspaces for all piles
///////////////////////
void CardPileGroup::SetNullSpaceDC(HDC _hNullDC)
{
	m_hNullSpaceDC = _hNullDC;
}

////////////////////////
//Animates every card when transferred between piles
///////////////////////
void CardPileGroup::Animate(std::list<Card*>* _pCardList, Vector2 _Dest)
{
	for(int i = 0; i < 5; i++)
	{		
		auto _cardIter = _pCardList->begin();
		//Create the vector to add to the card's position
		Vector2 _Increment = _Dest - (*_cardIter)->GetPos();
		_Increment /= 5;
		while(_cardIter != _pCardList->end())
		{	
			//Move the card by the increment vector then redraw scene
			(*_cardIter)->Move(_Increment);		
			_cardIter++;
		}
		Sleep(10);
		Game::GetGameInstance()->Draw();
		Game::GetGameInstance()->GetBackBuffer()->Present();
	}
}

////////////////////////
//Returns all piles of cards
///////////////////////
std::vector<std::list<Card*>*> CardPileGroup::GetStacks()
{
	return m_Stacks;
}